import { _decorator, Component, Node, animation } from 'cc';
const { ccclass, property } = _decorator;

enum MoveState {
    DEFAULT,
    IDE,
    WALK,
    RUN,
}


export class AnimationStateController {
    private _animationController: animation.AnimationController = null!;


    private _moveState: number = MoveState.DEFAULT;


    constructor(animationController: animation.AnimationController) {
        this._animationController = animationController;
    }

    public idle(): void {
        if (this._moveState === MoveState.IDE) return;
        this._animationController.setValue("idle", true);
        this._moveState = MoveState.IDE;
    }
    public walk(angle: number): void {
        this.setBlenderParam(angle);
        if (this._moveState === MoveState.WALK) return;
        this._animationController.setValue("walk", true);
        this._moveState = MoveState.WALK;
    }
    public run(angle: number): void {
        this.setBlenderParam(angle);
        if (this._moveState === MoveState.RUN) return;
        this._animationController.setValue("run", true);
        this._moveState = MoveState.IDE;
        this._moveState = MoveState.RUN;
    }

    private setBlenderParam(angle: number): void {
        //设置二维混合-前后参数
        let vMove: number = 1;
        if (Math.abs(angle) > 90) {
            vMove = -1;
        }
        //设置二维混合-左右参数
        let _angle: number = Math.abs(angle);
        _angle -= 90;
        _angle = Math.abs(_angle);
        let hMove = _angle / 90;
        hMove = 1.0 - hMove;
        if (vMove > 0) {
            if (angle > 0) {
                hMove *= -1;      //-1  0  1
            }
        } else {
            if (angle < 0) {
                hMove *= -1;      //1  0  -1
            }
        }
        this._animationController.setValue("v", vMove);
        this._animationController.setValue("h", hMove);


    }

}


